package com.zdpx.pxframework.core.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;

/**
 * 反射工具类
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022/5/24 9:08
 */
public class ReflectUtil {
    /**
     * 获取切入点所在方法上的注解对象信息
     *
     * @param joinPoint       切入点
     * @param annotationClass 注解class
     * @param <T>             注解对象
     * @return 注解信息
     */
    @SuppressWarnings("SameParameterValue")
    public static <T extends Annotation> T getMethodAnnotation(ProceedingJoinPoint joinPoint, Class<T> annotationClass) {
        return ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(annotationClass);
    }

    /**
     * 获取切入点所在类上的注解对象信息
     *
     * @param joinPoint       切入点
     * @param annotationClass 注解
     * @param <T>             注解
     * @return 注解信息
     */
    @SuppressWarnings("SameParameterValue")
    public static <T extends Annotation> T getClassAnnotation(ProceedingJoinPoint joinPoint, Class<T> annotationClass) {
        return ((MethodSignature) joinPoint.getSignature()).getMethod().getDeclaringClass().getAnnotation(annotationClass);
    }
}
